﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using umbraco.businesslogic;
using umbraco.cms.businesslogic.web;
using System.Globalization;

public partial class usercontrols_TeamViewControl_ContactControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void submitButton_Click(object sender, EventArgs e)
    {
        string name = NameBox.Text;
        string email = EmailBox.Text;
        string telephone = PhoneBox.Text;
        string question = QuestionBox.Text;
        string msn = MSNBox.Text;
        DateTime time = DateTime.Now;
        if (name == "")
            Response.Write("<script language='javascript'>alert('Name can not be empty.');</script>");
        else
        {
            if (email == "")
                Response.Write("<script language='javascript'>alert('Email can not be empty.');</script>");
            else
            {
                if (question == "")
                    Response.Write("<script language='javascript'>alert('Question can not be empty.');</script>");
                else
                {
                    CreateMessageDocument(name, email, telephone, question, msn, time);
                    Response.Redirect("/");
                    Response.Write("<script language='javascript'>alert('Your Message have sended success.');</script>");         
                }
            }
        }
    }
    protected void CreateMessageDocument(string name, string email, string telephone, string question, string msn, DateTime time)
    {
        string Year = time.ToString("yyyy", DateTimeFormatInfo.InvariantInfo);
        string Month = time.ToString("%M", DateTimeFormatInfo.InvariantInfo);
        string Day = time.ToString("%d", DateTimeFormatInfo.InvariantInfo);
        Document[] AllNode = Document.GetChildrenForTree(-1);
        foreach (Document TeamView in AllNode)
        {
            string NodeInformation = TeamView.ToString();
            if (NodeInformation.IndexOf("Text: TeamView") > -1)
            {
                Document[] TeamViewChildNode = Document.GetChildrenForTree(TeamView.Id);
                foreach (Document Mes in TeamViewChildNode)
                {
                    string mes = Mes.ToString();
                    if (mes.IndexOf("Text: Message") > -1)
                    {
                        Document[] MessageChildNode = Document.GetChildrenForTree(Mes.Id);
                        foreach (Document Y in MessageChildNode)
                        {
                            string y = Y.ToString();
                            if (y.IndexOf("Text: " + Year) > -1)
                            {
                                Document[] YChildNode = Document.GetChildrenForTree(Y.Id);
                                foreach (Document M in YChildNode)
                                {
                                    string m = M.ToString();
                                    if (m.IndexOf("Text: " + Month) > -1)
                                    {
                                        Document[] MChildNode = Document.GetChildrenForTree(M.Id);
                                        foreach (Document D in MChildNode)
                                        {
                                            string d = D.ToString();
                                            if (d.IndexOf("Text: " + Day) > -1)
                                            {
                                                Document[] DChildNode = Document.GetChildrenForTree(D.Id);
                                                foreach (Document emailNode in DChildNode)
                                                {
                                                    if (d.IndexOf("Text: " + email) > -1)
                                                    {
                                                    }
                                                }
                                                //Create Emaildocument
                                                Document EmailDocument5 = Document.MakeNew(email, DocumentType.GetByAlias("Email"), D.Creator, D.Id);
                                                EmailDocument5.getProperty("name").Value = name;
                                                EmailDocument5.getProperty("email").Value = email;
                                                EmailDocument5.getProperty("telephone").Value = telephone;
                                                EmailDocument5.getProperty("question").Value = question;
                                                EmailDocument5.getProperty("mSN").Value = msn;
                                                EmailDocument5.getProperty("time").Value = time.ToString();
                                                EmailDocument5.Publish(D.Creator);
                                                umbraco.library.UpdateDocumentCache(EmailDocument5.Id);
                                                umbraco.library.RefreshContent();
                                                return;
                                            }
                                        }
                                        //Create DayFolder
                                        Document DayFolder4 = Document.MakeNew(Day, DocumentType.GetByAlias("Folder"), M.Creator, M.Id);
                                        DayFolder4.Publish(M.Creator);
                                        umbraco.library.UpdateDocumentCache(DayFolder4.Id);
                                        umbraco.library.RefreshContent();
                                        //Create Emaildocument
                                        Document EmailDocument4 = Document.MakeNew(email, DocumentType.GetByAlias("Email"), DayFolder4.Creator, DayFolder4.Id);
                                        EmailDocument4.getProperty("name").Value = name;
                                        EmailDocument4.getProperty("email").Value = email;
                                        EmailDocument4.getProperty("telephone").Value = telephone;
                                        EmailDocument4.getProperty("question").Value = question;
                                        EmailDocument4.getProperty("mSN").Value = msn;
                                        EmailDocument4.getProperty("time").Value = time.ToString();
                                        EmailDocument4.Publish(DayFolder4.Creator);
                                        umbraco.library.UpdateDocumentCache(EmailDocument4.Id);
                                        umbraco.library.RefreshContent();
                                        return;
                                    }
                                }
                                //Create MonthFolder
                                Document MonthFolder3 = Document.MakeNew(Month, DocumentType.GetByAlias("Folder"), Y.Creator, Y.Id);
                                MonthFolder3.Publish(Y.Creator);
                                umbraco.library.UpdateDocumentCache(MonthFolder3.Id);
                                umbraco.library.RefreshContent();
                                //Create DayFolder
                                Document DayFolder3 = Document.MakeNew(Day, DocumentType.GetByAlias("Folder"), MonthFolder3.Creator, MonthFolder3.Id);
                                DayFolder3.Publish(MonthFolder3.Creator);
                                umbraco.library.UpdateDocumentCache(DayFolder3.Id);
                                umbraco.library.RefreshContent();
                                //Create Emaildocument
                                Document EmailDocument3 = Document.MakeNew(email, DocumentType.GetByAlias("Email"), DayFolder3.Creator, DayFolder3.Id);
                                EmailDocument3.getProperty("name").Value = name;
                                EmailDocument3.getProperty("email").Value = email;
                                EmailDocument3.getProperty("telephone").Value = telephone;
                                EmailDocument3.getProperty("question").Value = question;
                                EmailDocument3.getProperty("mSN").Value = msn;
                                EmailDocument3.getProperty("time").Value = time.ToString();
                                EmailDocument3.Publish(DayFolder3.Creator);
                                umbraco.library.UpdateDocumentCache(EmailDocument3.Id);
                                umbraco.library.RefreshContent();
                                return;
                            }
                        }
                        //Create YearFolder
                        Document YearFolder2 = Document.MakeNew(Year, DocumentType.GetByAlias("Folder"), Mes.Creator, Mes.Id);
                        YearFolder2.Publish(Mes.Creator);
                        umbraco.library.UpdateDocumentCache(YearFolder2.Id);
                        umbraco.library.RefreshContent();
                        //Create MonthFolder
                        Document MonthFolder2 = Document.MakeNew(Month, DocumentType.GetByAlias("Folder"), YearFolder2.Creator, YearFolder2.Id);
                        MonthFolder2.Publish(YearFolder2.Creator);
                        umbraco.library.UpdateDocumentCache(MonthFolder2.Id);
                        umbraco.library.RefreshContent();
                        //Create DayFolder
                        Document DayFolder2 = Document.MakeNew(Day, DocumentType.GetByAlias("Folder"), MonthFolder2.Creator, MonthFolder2.Id);
                        DayFolder2.Publish(MonthFolder2.Creator);
                        umbraco.library.UpdateDocumentCache(DayFolder2.Id);
                        umbraco.library.RefreshContent();
                        //Create Emaildocument
                        Document EmailDocument2 = Document.MakeNew(email, DocumentType.GetByAlias("Email"), DayFolder2.Creator, DayFolder2.Id);
                        EmailDocument2.getProperty("name").Value = name;
                        EmailDocument2.getProperty("email").Value = email;
                        EmailDocument2.getProperty("telephone").Value = telephone;
                        EmailDocument2.getProperty("question").Value = question;
                        EmailDocument2.getProperty("mSN").Value = msn;
                        EmailDocument2.getProperty("time").Value = time.ToString();
                        EmailDocument2.Publish(DayFolder2.Creator);
                        umbraco.library.UpdateDocumentCache(EmailDocument2.Id);
                        umbraco.library.RefreshContent();
                        return;
                    }
                }
                //Create MessageFolder
                Document MessageFolder1 = Document.MakeNew("Message", DocumentType.GetByAlias("Folder"), TeamView.Creator, TeamView.Id);
                MessageFolder1.getProperty("umbracoNaviHide").Value = "1";
                MessageFolder1.Publish(TeamView.Creator);
                umbraco.library.UpdateDocumentCache(MessageFolder1.Id);
                umbraco.library.RefreshContent();
                //Create YearFolder
                Document YearFolder1 = Document.MakeNew(Year, DocumentType.GetByAlias("Folder"), MessageFolder1.Creator, MessageFolder1.Id);
                YearFolder1.Publish(MessageFolder1.Creator);
                umbraco.library.UpdateDocumentCache(YearFolder1.Id);
                umbraco.library.RefreshContent();
                //Create MonthFolder
                Document MonthFolder1 = Document.MakeNew(Month, DocumentType.GetByAlias("Folder"), YearFolder1.Creator, YearFolder1.Id);
                MonthFolder1.Publish(YearFolder1.Creator);
                umbraco.library.UpdateDocumentCache(MonthFolder1.Id);
                umbraco.library.RefreshContent();
                //Create DayFolder
                Document DayFolder1 = Document.MakeNew(Day, DocumentType.GetByAlias("Folder"), MonthFolder1.Creator, MonthFolder1.Id);
                DayFolder1.Publish(MonthFolder1.Creator);
                umbraco.library.UpdateDocumentCache(DayFolder1.Id);
                umbraco.library.RefreshContent();
                //Create Emaildocument
                Document EmailDocument1 = Document.MakeNew(email, DocumentType.GetByAlias("Email"), DayFolder1.Creator, DayFolder1.Id);
                EmailDocument1.getProperty("name").Value = name;
                EmailDocument1.getProperty("email").Value = email;
                EmailDocument1.getProperty("telephone").Value = telephone;
                EmailDocument1.getProperty("question").Value = question;
                EmailDocument1.getProperty("mSN").Value = msn;
                EmailDocument1.getProperty("time").Value = time.ToString();
                EmailDocument1.Publish(DayFolder1.Creator);
                umbraco.library.UpdateDocumentCache(EmailDocument1.Id);
                umbraco.library.RefreshContent();
                return;
            }
        }
    }
}